//
//  PhotoCell.m
//  Version1.0_RectiveCocoa
//
//  Created by 吴可高 on 2018/1/20.
//  Copyright © 2018年 吴可高. All rights reserved.
//

#import "PhotoCell.h"
#import "AlbumModel.h"
#import "PhotoTool.h"

@interface PhotoCell()
@property (strong, nonatomic) UIImageView *coverView1;
@property (strong, nonatomic) UIImageView *coverView2;
@property (strong, nonatomic) UIImageView *coverView3;
@property (strong, nonatomic) UILabel *albumNameLb;
@property (strong, nonatomic) UILabel *photoNumberLb;
@property (assign, nonatomic) PHImageRequestID requestId1;
@property (assign, nonatomic) PHImageRequestID requestId2;
@property (assign, nonatomic) PHImageRequestID requestId3;
@end

@implementation PhotoCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setup];
    }
    return self;
}
-(void)setup{
    [self.contentView addSubview:self.coverView3];
    [self.contentView addSubview:self.coverView2];
    [self.contentView addSubview:self.coverView1];
    [self.contentView addSubview:self.albumNameLb];
    [self.contentView addSubview:self.photoNumberLb];
}

-(void)setModel:(AlbumModel *)model{
    self.albumNameLb.text = model.albumName;
    _model = model;
    NSInteger photoCount = model.result.count;
    if (!model.asset) {
        model.asset = model.result.lastObject;
    }
    self.photoNumberLb.text = @(photoCount).stringValue;
    __weak  typeof(self)weakSelf = self;
    self.requestId1 = [PhotoTool getImageWithAlbumModel:model size:CGSizeMake(CGRectGetHeight(self.frame) * 1.6,CGRectGetHeight(self.frame) * 1.6) completion:^(UIImage *image, AlbumModel * model) {
        if (weakSelf.model == model) {
            weakSelf.coverView1.image = image;
        }
    }];
    if (photoCount == 1) {
        self.coverView2.hidden = YES;
        self.coverView3.hidden = YES;
    }else if (photoCount == 2) {
        if (!model.asset2) {
            model.asset2 = model.result[1];
        }
       self.requestId2 = [PhotoTool getImageWithAlbumModel:model size:CGSizeMake(CGRectGetHeight(self.frame) * 0.7,CGRectGetHeight(self.frame) * 0.7) completion:^(UIImage *image, AlbumModel * model) {
            if (weakSelf.model == model) {
                weakSelf.coverView2.image = image;
            }
       }];
        self.coverView2.hidden = NO;
        self.coverView3.hidden = YES;
    }else {
        if (!model.asset2) {
            model.asset2 = model.result[1];
        }
        if (!model.asset3) {
            model.asset3 = model.result[2];
        }
        self.coverView2.hidden = NO;
        self.coverView3.hidden = NO;
        
        self.requestId2 = [PhotoTool getImageWithAlbumModel:model size:CGSizeMake(CGRectGetHeight(self.frame) * 0.7,CGRectGetHeight(self.frame) * 0.7) completion:^(UIImage *image, AlbumModel * model) {
            if (weakSelf.model == model) {
                weakSelf.coverView2.image = image;
            }
        }];
        self.requestId3 = [PhotoTool getImageWithAlbumModel:model size:CGSizeMake(CGRectGetHeight(self.frame) * 0.7,CGRectGetHeight(self.frame) * 0.7) completion:^(UIImage *image, AlbumModel * model) {
            if (weakSelf.model == model) {
                weakSelf.coverView3.image = image;
            }
        }];
    }
    
    self.albumNameLb.text = model.albumName;
    self.photoNumberLb.text = @(photoCount).stringValue;
    
    
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.coverView1.frame = CGRectMake(10, 5,CGRectGetHeight(self.frame) - 10, CGRectGetHeight(self.frame) - 10);
    if (self.model.count == 2) {
        self.coverView2.frame = CGRectMake(12.5, 3.5, CGRectGetHeight(self.frame) - 15, CGRectGetHeight(self.frame) - 15);
    }else {
        self.coverView2.frame = CGRectMake(12.5, 3.5, CGRectGetHeight(self.frame) - 15, CGRectGetHeight(self.frame) - 15);
        self.coverView3.frame = CGRectMake(15, 2, CGRectGetHeight(self.frame) - 20, CGRectGetHeight(self.frame) - 20);
    }
    CGFloat albumNameLbX = CGRectGetMaxX(self.coverView1.frame) + 12;
    CGFloat albumNameLbY = CGRectGetHeight(self.frame) / 2  - 16;
    self.albumNameLb.frame = CGRectMake(albumNameLbX, albumNameLbY, CGRectGetWidth(self.frame) - albumNameLbX - 40, 14);
    self.photoNumberLb.frame = CGRectMake(albumNameLbX, CGRectGetHeight(self.frame) / 2 + 2,CGRectGetWidth(self.frame), 13);
}
#pragma mark - < cell懒加载 >
- (UIImageView *)coverView1 {
    if (!_coverView1) {
        _coverView1 = [[UIImageView alloc] init];
        _coverView1.contentMode = UIViewContentModeScaleAspectFill;
        _coverView1.clipsToBounds = YES;
    }
    return _coverView1;
}
- (UIImageView *)coverView2 {
    if (!_coverView2) {
        _coverView2 = [[UIImageView alloc] init];
        _coverView2.contentMode = UIViewContentModeScaleAspectFill;
        _coverView2.clipsToBounds = YES;
    }
    return _coverView2;
}
- (UIImageView *)coverView3 {
    if (!_coverView3) {
        _coverView3 = [[UIImageView alloc] init];
        _coverView3.contentMode = UIViewContentModeScaleAspectFill;
        _coverView3.clipsToBounds = YES;
    }
    return _coverView3;
}
- (UILabel *)albumNameLb {
    if (!_albumNameLb) {
        _albumNameLb = [[UILabel alloc] init];
        _albumNameLb.textColor = [UIColor blackColor];
        _albumNameLb.font = [UIFont systemFontOfSize:13];
    }
    return _albumNameLb;
}
- (UILabel *)photoNumberLb {
    if (!_photoNumberLb) {
        _photoNumberLb = [[UILabel alloc] init];
        _photoNumberLb.textColor = [UIColor lightGrayColor];
        _photoNumberLb.font = [UIFont systemFontOfSize:12];
    }
    return _photoNumberLb;
}
@end
